package com.itwlj.mapper;

import com.itwlj.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressBookMapper {

    /**
     * 新增地址
     *
     * @param addressBook
     */
    @Insert("insert into address_book(user_id, receiver, contact, address, is_default) values " +
            "(#{userId},#{receiver},#{contact},#{address},#{isDefault})")
    void insert(AddressBook addressBook);

    /**
     * 根据用户id查询地址簿
     *
     * @param userId
     * @return
     */
    @Select("select id, receiver, contact, address, is_default from address_book where user_id = #{userId}")
    List<AddressBook> getByUserId(Long userId);

    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Delete("delete from address_book where id = #{id}")
    void deleteById(Long id);

    /**
     * 查询用户默认地址
     *
     * @param userId
     * @param isDefault
     * @return
     */
    @Select("select id, receiver, contact, address, is_default from address_book where user_id = #{userId} and is_default = #{isDefault}")
    AddressBook getDefaultAddress(Long userId, Integer isDefault);

    /**
     * 根据id查询地址簿
     *
     * @param id
     * @return
     */
    @Select("select receiver, contact, address from address_book where id = #{id}")
    AddressBook getById(Long id);
}
